/*
 * Filename:    CodeListController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月17日
 */
package com.paic.mhis.hcpms.common.community.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.common.community.biz.service.CommunityService;
import com.paic.mhis.hcpms.common.community.dto.CodeListDTO;
import com.paic.mhis.hcpms.common.community.dto.MedicalProviderReleDTO;
import com.paic.mhis.hcpms.common.community.web.vo.CommunityVO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 
 * @author EX-XIEQI001
 * 
 */
@Controller
@RequestMapping("/community/")
public class CommunityController {

	@BizServicesProxy(beanName = "communityService")
	public CommunityService communityService;

	@RequestMapping("getCodeListDatas.do")
	@ResponseBody
	public Map<String, Object> getCodeListDatas() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", communityService.getCodeListDatas());
		return map;

	}

	/**
	 * eg:<br>
	 * codes = "xx,xx2,xx3,xx4"
	 * 
	 * @param codes
	 * @return
	 */
	@RequestMapping(value = "getCodeListDatasWithParam.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCodeListDatasWithParam(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String codes = request.getParameter("codes");
		if (codes != null) {
			if (!"".equals(codes.trim())) {
				String[] tmr = codes.split(",");
				map.put("codeList", tmr);
				List<CodeListDTO> tlis = communityService.getCodeListDatas(map);
				map = new HashMap<String, Object>();
				map.put("list", tlis);
				return map;
			}
		}
		map.put("list", communityService.getCodeListDatas());
		return map;

	}

	/**
	 * 获取指定医疗机构下的省科室
	 */
	@RequestMapping("getProvincialOrgDepList.do")
	@ResponseBody
	public Map<String, Object> getProvincialOrgDepList(@RequestParam(value = "warnNo") String warnNo) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("warnNo", StringUtils.isEmpty(warnNo) ? null : warnNo);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", communityService.getProvincialOrgDepList(paramMap));
		return map;
	}

	/**
	 * 医院科室信息 param department 省科室
	 * 
	 * @return
	 */
	@RequestMapping("getWorkDepList.do")
	@ResponseBody
	public Map<String, Object> getWorkDepList(@RequestParam(value = "warnNo") String warnNo,
			@RequestParam(value = "departMent") String departMent) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("warnNo", StringUtils.isEmpty(warnNo) ? null : warnNo);
		paramMap.put("departMent", StringUtils.isEmpty(departMent) ? null : departMent);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", communityService.getWorkDepList(paramMap));
		return map;
	}

	/**
	 * 机构信息
	 * */
	@RequestMapping("getMedicalOrgInfoList.do")
	@ResponseBody
	public Map<String, Object> getMedicalOrgInfoList(@ModelAttribute CommunityVO vo, HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		Map<String, Object> paramMap = new HashMap<String, Object>();

		// 医疗机构级别大类
		if (null != vo && null != vo.getOrgLevelList() && vo.getOrgLevelList().size() > 0) {
			paramMap.put("orgLevelList", vo.getOrgLevelList());
		}
		// 医疗机构类型
		if (null != vo && null != vo.getOrgTypeList() && vo.getOrgTypeList().size() > 0) {
			paramMap.put("orgTypeList", vo.getOrgTypeList());
		}
		// 医疗机构级别
		if (null != vo && null != vo.getMedicalOrgLevelList() && vo.getMedicalOrgLevelList().size() > 0) {
			paramMap.put("medicalOrgLevelList", vo.getMedicalOrgLevelList());
		}
		paramMap.put("areaList", user.getAreaInfoList());
		paramMap.put("medicalOrgList", communityService.getMedicalOrgInfoList(paramMap));
		return paramMap;
	}

	/**
	 * 机构信息
	 * */
	@RequestMapping("getNewMedicalOrgInfoList.do")
	@ResponseBody
	public Map<String, Object> getNewMedicalOrgInfoList(@ModelAttribute CommunityVO vo, HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		Map<String, Object> paramMap = new HashMap<String, Object>();

		// 医疗机构级别大类
		if (null != vo && null != vo.getOrgLevelList() && vo.getOrgLevelList().size() > 0) {
			paramMap.put("orgLevelList", vo.getOrgLevelList());
		}
		// 医疗机构类型
		if (null != vo && null != vo.getOrgTypeList() && vo.getOrgTypeList().size() > 0) {
			paramMap.put("orgTypeList", vo.getOrgTypeList());
		}
		// 医疗机构级别
		if (null != vo && null != vo.getMedicalOrgLevelList() && vo.getMedicalOrgLevelList().size() > 0) {
			paramMap.put("medicalOrgLevelList", vo.getMedicalOrgLevelList());
		}
		if (null != vo && null != vo.getProviderType()) {
			paramMap.put("providerType", vo.getProviderType());
		}
		if (null != vo && null != vo.getGrade() && vo.getGrade().length() > 0) {
			paramMap.put("grade", vo.getGrade());
		}
		if (null != vo && null != vo.getAreaCode() && vo.getAreaCode().length() > 0) {
			paramMap.put("areaCode", vo.getAreaCode());
		}
		paramMap.put("userName", user.getUsername());
		paramMap.put("areaList", user.getAreaInfoList());
		paramMap.put("medicalOrgList", communityService.getNewMedicalOrgInfoList(paramMap));
		return paramMap;
	}

	/**
	 * 机构信息(综合评价一级及以下)
	 * */
	@RequestMapping("getNewMedicalOrgInfoOtherList.do")
	@ResponseBody
	public Map<String, Object> getNewMedicalOrgInfoOtherList(@ModelAttribute CommunityVO vo, HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		Map<String, Object> paramMap = new HashMap<String, Object>();

		// 医疗机构级别大类
		if (null != vo && null != vo.getOrgLevelList() && vo.getOrgLevelList().size() > 0) {
			paramMap.put("orgLevelList", vo.getOrgLevelList());
		}
		// 医疗机构类型
		if (null != vo && null != vo.getOrgTypeList() && vo.getOrgTypeList().size() > 0) {
			paramMap.put("orgTypeList", vo.getOrgTypeList());
		}
		// 医疗机构级别
		if (null != vo && null != vo.getMedicalOrgLevelList() && vo.getMedicalOrgLevelList().size() > 0) {
			paramMap.put("medicalOrgLevelList", vo.getMedicalOrgLevelList());
		}
		if (null != vo && null != vo.getProviderType()) {
			paramMap.put("providerType", vo.getProviderType());
		}
		if (null != vo && null != vo.getGrade() && vo.getGrade().length() > 0) {
			paramMap.put("grade", vo.getGrade());
		}
		paramMap.put("userName", user.getUsername());
		paramMap.put("areaList", user.getAreaInfoList());
		paramMap.put("medicalOrgList", communityService.getNewMedicalOrgInfoList(paramMap));
		return paramMap;
	}

	/**
	 * add by xf：获取所有的机构信息，前面的有方法中的机构等级时显示的等级编码，
	 * 界面上要显示等级名称，本来想修改上面那个方法，但那个方法被很多地方调用,为了不影响其他地方，在这里新增加一个方法，对返回列表中的
	 * Level进行下转化，具体的转化规则是：根据等级编码和hcpms_code_list表中类型为medical_org_level的医疗机构数据字典
	 * 参考sql如下 left join hcpms_code_list c on c.code_no = tt.hospital_level and
	 * c.code_type = 'medical_org_level'
	 */
	@RequestMapping("getMedicalOrgInfoListWithLevel.do")
	@ResponseBody
	public Map<String, Object> getMedicalOrgInfoListWithLevel(HttpServletRequest request) {
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("medicalOrgList", communityService.getMedicalOrgInfoListWithLevel(paramMap));

		return paramMap;
	}

	/**
	 * 科室信息
	 * */
	@RequestMapping("getMedicalDeptInfoList.do")
	@ResponseBody
	public Map<String, Object> getMedicalDeptInfoList(@ModelAttribute CommunityVO vo, HttpServletRequest request) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (null != vo && StringUtils.isNotBlank(vo.getOrgCode())) {
			paramMap.put("orgCode", vo.getOrgCode());
			paramMap.put("medicalDeptList", communityService.getMedicalDeptInfoList(paramMap));
		} else if (null != vo && StringUtils.isNotBlank(vo.getOrgId())) {
			paramMap.put("orgId", vo.getOrgId());
			paramMap.put("medicalDeptList", communityService.getMedicalDeptInfoList(paramMap));
		}
		return paramMap;
	}

	/**
	 * 科室信息
	 * */
	@RequestMapping("queryProvincialInfoByHosCode.do")
	@ResponseBody
	public List<MedicalProviderReleDTO> queryProvincialInfoByHosCode(
			@RequestParam(value = "hosCode", defaultValue = "") String hosCode, HttpServletRequest request) {
		if (!StringUtils.isNotBlank(hosCode)) {
			return new ArrayList<MedicalProviderReleDTO>();
		}

		Map paramMap = new HashMap();
		paramMap.put("providerCode", hosCode);
		return communityService.queryProvincialInfoByHosCode(paramMap);
	}

}
